#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , settings(new QSettings("Kiyose","mini_menu"))
{

    ui->setupUi(this);
    updateCount();
    viewEntries();
}

// 添加新条目到指定类别中，避免重复
void MainWindow::addEntryToCategory(const QString &category, const QString &newEntry)
{
    // 获取当前存储的条目列表
    QStringList entries = settings->value(category).toStringList();

    // 如果新条目不存在，则添加
    if (!entries.contains(newEntry)) {
        entries << newEntry;
        settings->setValue(category, entries);
        qDebug() << "新条目已添加到" << category << ":" << newEntry;
    } else {
        qDebug() << "条目已经存在于" << category << "中，无需重复添加:" << newEntry;
    }
    updateCount();
    viewEntries();
}

// 从指定类别中读取并打印所有条目
void MainWindow::printEntriesFromCategory(const QString &category)
{
    QStringList entries = settings->value(category).toStringList();
    if (entries.isEmpty()) {
        qDebug() << category << "为空.";
    } else {
        qDebug() << "当前存储在" << category << "中的条目列表:";
        for (const QString &entry : qAsConst(entries)) {
            qDebug() << "- " << entry;
        }
    }
}

MainWindow::~MainWindow()
{
    delete settings; // 释放 QSettings 资源
    delete ui;
}

void MainWindow::on_addMeat_btn_clicked()
{
    if(!(ui->meat_Edit->text().isEmpty()))
        addEntryToCategory("main/dish/meat", ui->meat_Edit->text());
    ui->meat_Edit->clear();
}

void MainWindow::on_addVeg_btn_clicked()
{
    if(!(ui->veg_Edit->text().isEmpty()))
        addEntryToCategory("main/dish/veg", ui->veg_Edit->text());
    ui->veg_Edit->clear();
}


void MainWindow::on_addFru_btn_clicked()
{
    if(!(ui->fru_Edit->text().isEmpty()))
        addEntryToCategory("main/dish/fru",ui->fru_Edit->text());
    ui->fru_Edit->clear();
}

void MainWindow::on_addOth_btn_clicked()
{
    if(!(ui->oth_Edit->text().isEmpty()))
        addEntryToCategory("main/dish/oth",ui->oth_Edit->text());
    ui->oth_Edit->clear();
}

void MainWindow::updateCount()
{
    // 读取条目列表
    QStringList meatDishes = settings->value("main/dish/meat").toStringList();
    int countMeat = meatDishes.size();
    QStringList vegDishes = settings->value("main/dish/veg").toStringList();
    int countVeg = vegDishes.size();
    QStringList fruDishes = settings->value("main/dish/fru").toStringList();
    int countFru = fruDishes.size();
    QStringList othDishes = settings->value("main/dish/oth").toStringList();
    int countOth = othDishes.size();
    int count = countMeat+countFru+countVeg+countOth;;

    // 更新统计文本
    ui->count_all->setText(QString("%1").arg(count));
    ui->count_meat->setText(QString("%1").arg(countMeat));
    ui->count_veg->setText(QString("%1").arg(countVeg));
    ui->count_fru->setText(QString("%1").arg(countFru));
    ui->count_oth->setText(QString("%1").arg(countOth));

}

void MainWindow::viewEntries()
{
    // 清空 QListWidget
    ui->listmeat->clear();
    ui->listveg->clear();
    ui->listfru->clear();
    ui->listother->clear();

    // 读取条目列表
    QStringList meatDishes = settings->value("main/dish/meat").toStringList();
    QStringList vegDishes = settings->value("main/dish/veg").toStringList();
    QStringList fruDishes = settings->value("main/dish/fru").toStringList();
    QStringList othDishes = settings->value("main/dish/oth").toStringList();


    // 将每个条目添加到 QListWidget 中
    for (const QString &entry : qAsConst(meatDishes)) {
        new QListWidgetItem(entry, ui->listmeat);
    }
    for (const QString &entry : qAsConst(vegDishes)) {
        new QListWidgetItem(entry, ui->listveg);
    }
    for (const QString &entry : qAsConst(fruDishes)) {
        new QListWidgetItem(entry, ui->listfru);
    }
    for (const QString &entry : qAsConst(othDishes)) {
        new QListWidgetItem(entry, ui->listother);
    }

    // 如果没有条目，显示提示信息
    if (meatDishes.isEmpty()) {
        ui->listmeat->addItem("暂无荤菜");
    }
    if (vegDishes.isEmpty()) {
        ui->listveg->addItem("暂无素菜");
    }
    if (fruDishes.isEmpty()) {
        ui->listfru->addItem("暂无水果");
    }
    if (othDishes.isEmpty()) {
        ui->listother->addItem("暂无加餐");
    }
}

QString MainWindow::randomEntryFromCategory(const QString &category)
{
    // 获取当前存储的条目列表
    QStringList entries = settings->value(category).toStringList();

    if (entries.isEmpty()) {
        qDebug() << "类别" << category << "为空，无法随机选择条目。";
        return QString(); // 或者可以抛出异常或返回其他提示信息
    }

    // 生成一个随机索引
    int randomIndex = QRandomGenerator::global()->bounded(entries.size());

    // 返回随机选择的条目
    return entries.at(randomIndex);
}


void MainWindow::on_rangeMenu_btn_clicked()
{
    on_changeFru_btn_clicked();
    on_changeMeat_btn_clicked();
    on_changeVeg_btn_clicked();
    on_changeOth_btn_clicked();
}

void MainWindow::on_changeMeat_btn_clicked()
{
    QString randomEntry = randomEntryFromCategory("main/dish/meat");
    if (!randomEntry.isEmpty()) {
        ui->meat->setText(randomEntry);
    } else {
        ui->meat->setText("没菜");
    }
}


void MainWindow::on_changeVeg_btn_clicked()
{
    QString randomEntry = randomEntryFromCategory("main/dish/veg");
    if (!randomEntry.isEmpty()) {
        ui->veg->setText(randomEntry);
    } else {
        ui->veg->setText("没菜");
    }
}


void MainWindow::on_changeFru_btn_clicked()
{
    QString randomEntry = randomEntryFromCategory("main/dish/fru");
    if (!randomEntry.isEmpty()) {
        ui->fru->setText(randomEntry);
    } else {
        ui->fru->setText("没菜");
    }
}

void MainWindow::on_changeOth_btn_clicked()
{
    QString randomEntry = randomEntryFromCategory("main/dish/oth");
    if (!randomEntry.isEmpty()) {
        ui->other->setText(randomEntry);
    } else {
        ui->other->setText("没菜");
    }
}

